window.onload = init;

var oldDiv;
var oldCol
var keyarray;
var GEarray = new Array();;
var deptarray =  new Array();


function keywordSearch() {
   PageIndex = document.formKeyword.selectKeyword.selectedIndex;
   if (document.formKeyword.selectKeyword.options[PageIndex].value == "all")
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(true)
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }
   else
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(getKeyword(oldCol[x], document.formKeyword.selectKeyword.options[PageIndex].value))
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }


}

function GESearch() {
   PageIndex = document.formGE.selectGE.selectedIndex;
   //alert(document.formGE.selectGE.options[PageIndex].value);
   if(document.formGE.selectGE.options[PageIndex].value == "all")
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(true)
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }
   else
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(getGE(oldCol[x].innerHTML).match(document.formGE.selectGE.options[PageIndex].value))
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }
   
}
function deptSearch() {
   PageIndex = document.formDept.selectDept.selectedIndex;
   if(document.formDept.selectDept.options[PageIndex].value == "all")
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(true)
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }
   else
   {
      var tmp = " ";
      for(x in oldCol)
      {
         if(getDept(oldCol[x].innerHTML).match(document.formDept.selectDept.options[PageIndex].value))
         {
            tmp = tmp + " <div class=\"widget\"> " + (oldCol[x].innerHTML) + " </div> ";
         }
      }
      var content = getElementsByClassName("content", null);
      content[0].innerHTML = tmp;
   }

}
function getElementsByClassName(classname, node) {
   if(!node) node = document.getElementsByTagName("body")[0];
   var a = [];
   var re = new RegExp('\\b' + classname + '\\b');
   var els = node.getElementsByTagName("*");
   for(var i=0,j=els.length; i<j; i++)
   if(re.test(els[i].className))a.push(els[i]);
   return a;
  
}
function getKeyword(html, kw)
{
   var ret = "";
   ret = (html.id) + " ";
   //ret = ret.slice(ret.search("::kw:") + 4);
   ret = ret.toUpperCase().indexOf(kw.toUpperCase())
   return ret > -1;
}
function getGE(html)
{
   var ret = html;
   ret = ret.split("<strong>")[2];
   ret = ret.replace("GE Area:</strong>\n", '');
   ret = ret.slice(48,51);
   return ret;
}
function getDept(html)
{
   var ret;
   ret = html.split('-')[0];
   ret = ret.replace('<strong>','');
   var x = 0;
   while(x < 3) {
   	ret = ret.replace('0','');
   	ret = ret.replace('1','');
   	ret = ret.replace('2','');
   	ret = ret.replace('3','');
   	ret = ret.replace('4','');
   	ret = ret.replace('5','');
   	ret = ret.replace('6','');
   	ret = ret.replace('7','');
   	ret = ret.replace('8','');
   	ret = ret.replace('9','');
      x = x + 1;
   }
   return ret;
}

function arrayContains(array, n) {
   for(x in array)
   {
      if(array[x].match(n))
         return true;
   }
   return false;
}

function init() {
   
   var element = document.getElementById('keywordList');
   var keywords = element.innerHTML;
   keyarray = keywords.split(',');
   var col = getElementsByClassName("widget", null);
   oldDiv = getElementsByClassName("content", null)[0];
   oldCol = col;
   for(x in col)
   {
      var name = getDept(col[x].innerHTML);
      if(!arrayContains(deptarray, name))
      {
          deptarray.push(name);
      }
      
   }
   for(x in col)
   {

      var name = getGE(col[x].innerHTML);
      if(!arrayContains(GEarray, name))
      {
         GEarray.push(name);
         //alert(name);
      }
      
      
   } 
   GEarray = GEarray.sort()


   element = (document.getElementsByName('sidebar'));
   var ulz = getElementsByClassName("sidebar");
   for(x in ulz)
   {
      var keys = " ";
      for(y in keyarray)
      {

         if( y != keyarray.length - 1) {
            keys = keys + " <option value=\"" + keyarray[y] + "\" selected=\"selected\"> " + keyarray[y];
         }
      }
  
      var keyText = "Keyword: <br />" + 
      " <form id=\"formKeyword\" name=\"formKeyword\"> " +
      " <select id=\"selectKeyword\" size=\"1\"> " +
      " <option value=\"all\" selected=\"selected\"> " + " All keywords        " +
      keys +
      " <input type=\"button\" name=\"B2\" id=\"B2\" value=\"Go\" onclick=\"keywordSearch()\" />" +
      " </form>";
  
      var ges = " ";
      for(y in GEarray)
      {

         ges = ges + " <option value=\"" + GEarray[y] + "\" selected=\"selected\"> " + GEarray[y];
      }
  
      var GEText = "<br /> GEs: <br />" + 
      " <form id=\"formGE\" name=\"formGE\"> " +
      " <select id=\"selectGE\" size=\"1\"> " +
      " <option value=\"all\" selected=\"selected\"> " + " All GEs        " +
      ges +
      " <input type=\"button\" name=\"B3\" id=\"B3\" value=\"Go\" onclick=\"GESearch()\" />" +
      " </form>";

      var deps = " ";
      for(y in deptarray)
      {

         deps = deps + " <option value=\"" + deptarray[y] + "\" selected=\"selected\"> " + deptarray[y];
      }
  
      var deptText= "<br /> Departments: <br />" + 
      " <form id=\"formDept\" name=\"formDept\"> " +
      " <select id=\"selectDept\" size=\"1\"> " +
      " <option value=\"all\" selected=\"selected\"> " + " All Departments        " +
      deps +
      " <input type=\"button\" name=\"B4\" id=\"B4\" value=\"Go\" onclick=\"deptSearch()\" />" +
      " </form>";


      ulz[x].innerHTML = keyText + GEText + deptText;
      document.formKeyword.selectKeyword.selectedIndex = 0;
	document.formGE.selectGE.selectedIndex = 0;
      document.formDept.selectDept.selectedIndex = 0;
   }
   ulz = getElementsByClassName("header");
   for(x in ulz)
   {
     ulz[x].innerHTML = " ";
   }
   ulz = getElementsByClassName("subheader");
   for(x in ulz)
   {
     ulz[x].innerHTML = "You have returned " + col.length + " results. ";
   }
}



//animals,computers,people,planes,cars,botany,

/*

   for(x in GEarray)
   {
      alert(GEarray[x]);
   }

   //alert(arrayContains(GEarray, "CSC "));

   for(x in deptarray)
   {
      alert(deptarray[x]);
   }
  


   for(x in keyarray)
   {
      if( x != keyarray.length - 1)
      {
        alert(keyarray[x]);
      }
   }

*/